package yf.mall.controller.api.app;

import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.web.bind.annotation.*;
import yf.mall.component.app.AppMaterialComponent;
import yf.mall.entity.AppCategoryEntity;
import yf.mall.enums.app.AppCategoryTypeEnum;
import yf.mall.service.app.AppCategoryService;
import yf.mall.vo.app.common.ClassificationVo;
import yf.mall.vo.app.request.CommonRequest;
import yf.mall.vo.app.common.ListResult;
import yf.mall.vo.app.request.AppMaterialListRequest;
import yf.mall.vo.app.material.AppProductMaterialVo;
import yf.mall.vo.app.material.AppVideoMaterialVo;
import yf.mall.vo.app.request.ProductListRequest;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/app/mini/meterial")
public class ApiAppMaterialController extends ApiAppBaseController {

    @Resource
    private AppMaterialComponent appMaterialComponent;

    @Resource
    private AppCategoryService appCategoryService;


    @RequestMapping(value = "/classification",
            method = RequestMethod.POST)
    @IgnoreAuth
    public R SelectionClassification() {
        AppCategoryEntity rootCategory = appCategoryService.getRootCategory(AppCategoryTypeEnum.MATERIAL_TAB_ROOT.getType());
        if (rootCategory == null) {
            return R.ok(Collections.emptyList());
        }
        List<AppCategoryEntity> appCategoryEntities = appCategoryService.getCategoryByParent(rootCategory.getType(), rootCategory.getRootId());
        return dataResult(appCategoryEntities.stream().map(ClassificationVo::build).collect(Collectors.toList()));
//        return dataResult(new ArrayList<>());
    }

    @RequestMapping(value = "/detail",
            produces = {"application/json"},
            method = RequestMethod.POST)
    public R materialDetail(@LoginUserId Long userId, @RequestBody CommonRequest request) {
        Long pid = request.getId();
        AppProductMaterialVo productMaterialVo = appMaterialComponent.getProductMaterialDetail(pid, userId);
        return dataResult(productMaterialVo);
    }


    @RequestMapping(value = "/list",
            produces = {"application/json"},
            method = RequestMethod.POST)
    @IgnoreAuth
    public R materialList(@Valid @RequestBody(required = true) ProductListRequest request) {
        ListResult<AppProductMaterialVo> productMaterialList = appMaterialComponent.getProductMaterialList(request);
        return dataResult(productMaterialList);
    }


    @RequestMapping(value = "/original/classification",
            produces = {"application/json"},
            method = RequestMethod.POST)
    @IgnoreAuth
    public R materialOriginalClassification() {
        AppCategoryEntity rootCategory = appCategoryService.getRootCategory(AppCategoryTypeEnum.ORIGINAL_MATERIAL_ROOT.getType());
        if (rootCategory == null) {
            return R.ok(Collections.emptyList());
        }
        List<AppCategoryEntity> appCategoryEntities = appCategoryService.getCategoryByParent(rootCategory.getType(), rootCategory.getRootId());
        return dataResult(appCategoryEntities.stream().map(ClassificationVo::build).collect(Collectors.toList()));
    }


    @RequestMapping(value = "/original/material-list",
            method = RequestMethod.POST)
    public R materialOriginalMaterialList(@Valid @RequestBody(required = true) AppMaterialListRequest request) {
        ListResult<AppVideoMaterialVo> videoMaterialList = appMaterialComponent.getVideoMaterialList(request);
        return dataResult(videoMaterialList);
    }

}
